home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 1.iso
/
toolbox
/
src
/
exampleCode
/
opengl
/
render2pixmap
/
util.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-11-11
|
5KB
|
187 lines
/*
* Copyright (c) 1993-94, Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that the name of Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
* POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* OpenGL(TM) is a trademark of Silicon Graphics, Inc.
*/
/*
* 1993 Simon Hui -- Silicon Graphics Computer Systems
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
#include <X11/keysym.h>
#include "util.h"
static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
{
if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
return GL_TRUE;
}
return GL_FALSE;
}
Window utilCreateWindow( int defW, int defH, int defX, int defY, Window parent,
int attributes[], char *colorName,
int argc, char **argv, char *geometry,
Display **dpy_ret, XVisualInfo **vi_ret,
Colormap *cmap_ret, GC *xgc_ret )
{
Display *dpy;
Window window;
XVisualInfo *vi;
Colormap cmap;
GC xgc;
XSetWindowAttributes swa;
unsigned int width, height;
XGCValues gcvalues;
XColor xcolor, exact;
XSizeHints sh;
XEvent event;
if (dpy_ret && *dpy_ret) {
dpy = *dpy_ret;
} else {
dpy = XOpenDisplay(0);
if (!dpy) {
fprintf(stderr, "Can't connect to display \"%s\"\n",
getenv("DISPLAY"));
exit(1);
}
}
vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributes);
if (!vi) {
fprintf(stderr, "Cannot find visual on \"%s\"\n",
getenv("DISPLAY"));
exit(1);
}
if (cmap_ret && *cmap_ret) {
cmap = *cmap_ret;
} else {
cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
AllocNone);
}
if (colorName) {
if (!XAllocNamedColor(dpy, cmap, colorName, &exact, &xcolor)) {
fprintf(stderr, "Can't allocate color %s for X\n", colorName);
exit(1);
}
swa.background_pixel = xcolor.pixel;
} else {
swa.background_pixel = 0;
}
swa.border_pixel = 0;
swa.colormap = cmap;
swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
| KeyReleaseMask;
width = defW;
height = defH;
sh.x = defX;
sh.y = defY;
if (geometry) {
int x, y;
unsigned int w, h;
int mask = XParseGeometry(geometry, &x, &y, &w, &h);
if (mask & XValue) sh.x = x;
if (mask & YValue) sh.y = y;
if (mask & WidthValue) width = w;
if (mask & HeightValue) height = h;
}
sh.flags = USPosition | PPosition;
if (parent == None) parent = RootWindow(dpy, vi->screen);
window = XCreateWindow(dpy, parent, sh.x, sh.y,
width, height,
0, vi->depth, InputOutput, vi->visual,
CWBorderPixel|CWColormap|CWEventMask|CWBackPixel,
&swa);
XSetStandardProperties(dpy, window, argv[0], argv[0],
None, argv, argc, &sh);
XSetWMColormapWindows(dpy, window, &window, 1);
XMapWindow(dpy, window);
XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
if (!xgc_ret || !*xgc_ret) {
gcvalues.background = xcolor.pixel;
gcvalues.foreground = xcolor.pixel;
xgc = XCreateGC(dpy, window, GCForeground|GCBackground, &gcvalues);
}
if (dpy_ret) *dpy_ret = dpy;
if (vi_ret) *vi_ret = vi;
if (cmap_ret) *cmap_ret = cmap;
if (xgc_ret) *xgc_ret = xgc;
return window;
}
void utilEventLoop( Display *dpy,
void (*exposeCallback)(void),
void (*keyCallback)( KeySym, Bool *exit, Bool *redraw, Bool *tofile),
void (*displayCallback)(void),
void (*fileCallback)(void) )
{
XEvent event;
Bool redraw = False;
Bool tofile = False;
for (;;) {
do {
XNextEvent(dpy, &event);
switch (event.type) {
case Expose:
if (exposeCallback) {
(*exposeCallback)();
} else {
(*displayCallback)();
}
break;
case KeyPress:
{
char buf[100];
int rv;
KeySym ks;
Bool exit = False;
rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
(*keyCallback)(ks, &exit, &redraw, &tofile);
if (exit) return;
}
break;
}
} while (XPending(dpy) != 0);
if (redraw) {
(*displayCallback)();
redraw = False;
}
if (tofile) {
(*displayCallback)();
(*fileCallback)();
tofile = False;
}
}
}